<?php
// +----------------------------------------------------------------------

namespace app\mall\model;

use think\Model as ThinkModel;
use util\Tree;
use think\facade\Db;

/**
 * 商品模型
 * @package app\cms\model
 */
class Goods extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'mall_goods';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 获取商品列表
     * @return array|mixed
     */
    public static function getList($map = [], $order = [])
    {
        $data_list = self::view('mall_goods', true)
            ->view("mall_category", ['title' => 'category_name'], 'mall_category.id=mall_goods.cid', 'left')
            ->where($map)
            ->order($order)
            ->paginate(config('paginate'));
		// foreach($data_list as $key=>$value){
		// 	self::update(['id'=>$value['id'], 'pack_share' => $value['pro_share']]);
		// }
        return $data_list;
    }
    /**
     * 获取商品仓库库存
     * @return array|mixed
     */
    public static function getNums($map = [], $order = [], $eid = 0)
    {
        $data_list = self::view('mall_goods', true)
            ->view("mall_category", ['title' => 'category_name'], 'mall_category.id=mall_goods.cid', 'left')
            ->where($map)
            ->order($order)
            ->paginate(config('paginate'));
		$express = Db::name('mall_express')->where('id', $eid)->value('title');
		foreach($data_list as $key=>$value){
			$stock = json_decode($value['stock'], true);
			if (isset($stock[$eid])){
				$data_list[$key]['number'] = $stock[$eid];
			}else{
				$data_list[$key]['number'] = 0;
			}
			$data_list[$key]['stock'] = $express;
		}
        return $data_list;
    }
    /**
     * 获取单篇文档
     * @param string $id 文档id
     * @return array
     */
    public static function getOne($id = '')
    {
        return Db::view('mall_goods', true)
            ->view("mall_category", ['title' => 'category_name'], 'mall_category.id=mall_goods.cid', 'left')
            ->where('mall_goods.id', $id)
            ->find();
    }
	/**
	 * 设置分仓库存
	 * @param int $id 商品id
	 * @param int $eid 分仓id
	 * @param int $num 数值
	 * @param int $type 模式分类：inc：增加|dec:减少|edit:编辑
	 * @return bool|number
	 */
	public function setStock($id = 0, $eid = 0, $num = 1, $type = 'edit')
	{
		if ($id == 0 || $eid == 0) {
			$this->error = '设置库存参数错误';
			return false;
		}
	    $goods = self::where('id', $id)->find();
		$stock = json_decode($goods['stock'], true);
		if (!isset($stock[$eid])) $stock[$eid] = 0;
		switch ($type) {
			case 'inc' :
			$stock[$eid] = $stock[$eid] + $num;
			break;
			case 'dec' :
			$stock[$eid] = $stock[$eid] - $num;
			break;
			default:
			$stock[$eid] = $num;
		}
		$goods->stock = json_encode($stock);
		$goods->number = array_sum($stock);
		$goods->save();
		return $stock[$eid];
	}
	/**
	 * 获取分仓库存
	 * @param int $id 商品id
	 * @param int $eid 分仓id
	 * @return bool|number
	 */
	public static function getStock($id = 0, $eid = 0)
	{
		if ($id == 0 || $eid == 0) {
			return 0;
		}
	    $goods = self::where('id', $id)->find();
		$stock = json_decode($goods['stock'], true);
		if (!isset($stock[$eid])) $stock[$eid] = 0;
		
		return $stock[$eid];
	}
	/**
	 * 获取IDS
	 * @param string $id 文档id
	 * @return array
	 */
	public static function getids()
	{
	    return self::where('status', 1)->column('title','id');
	}
    
}
